#include "stdafx.h"
#include "func_btn.h"
#include "ypControllerDlg.h"
#include "network_sender.h"


BEGIN_MESSAGE_MAP(func_image_btn, CImageButton)
	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

void func_image_btn::OnLButtonDown(UINT nFlags, CPoint point)
{
	for (auto iter=m_vTrigger.begin(); 
		iter!=m_vTrigger.end(); iter++)
	{
		switch (iter->trigger_type())
		{
		case trigger::SWITCH_PAGE:
			{
				CypControllerDlg *pDialog = (CypControllerDlg *)AfxGetApp()->m_pMainWnd;
				pDialog->switch_page(iter->get_cmd_string());
			}
			break;
		case trigger::SEND_UDP:
			{
				network_sender *pSender = network_sender::get_instance();
				assert(pSender!=nullptr); 
				pSender->set_desti_endpoint(iter->get_udp_desti_ip(),
					iter->get_udp_desti_port());
				pSender->send_cmd(iter->get_cmd_string());
			}
			break;
		default:
			assert(!"undefined trigger type!");
			break;
		}
	}

	//CImageButton::OnLButtonDown(nFlags, point);
}

/////////////////////////////////////////////////////////////////////////
//func_text_btn
BEGIN_MESSAGE_MAP(func_text_btn, CButton)
	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

void func_text_btn::OnLButtonDown(UINT nFlags, CPoint point)
{
	for (auto iter=m_vTrigger.begin(); 
		iter!=m_vTrigger.end(); iter++)
	{
		switch (iter->trigger_type())
		{
		case trigger::SWITCH_PAGE:
			{
				CypControllerDlg *pDialog = (CypControllerDlg *)AfxGetApp()->m_pMainWnd;
				pDialog->switch_page(iter->get_cmd_string());
			}
			break;
		case trigger::SEND_UDP:
			{
				network_sender *pSender = network_sender::get_instance();
				assert(pSender!=nullptr); 
				pSender->set_desti_endpoint(iter->get_udp_desti_ip(),
					iter->get_udp_desti_port());
				pSender->send_cmd(iter->get_cmd_string());
			}
			break;
		default:
			assert(!"undefined trigger type!");
			break;
		}
	}

}

void func_text_btn::set_text(con_str_ref text)
{
	SetWindowText(CString(text.c_str()));
}